﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using SystemDALInterface;
using SystemModels;
using System.Data;
using System.Reflection;//引用反射

namespace SystemDAL
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public class MenuDAL : IMenuManager
    {
        DbContext db = new DbContext();
        #region 查询所有的菜单项
        /// <summary>
        /// 查询所有的菜单项
        /// </summary>
        /// <returns>菜单集合</returns>
        public List<Programs> QueryMenusAll()
        {
            //所有菜单项集合
            List<Programs> list = new List<Programs>();
            SqlDataReader dr = db.ExecReader("select * from Program");
            while (dr.Read())
            {
                Programs menu = new Programs();
                menu.ProgramId = (int)dr["ProgramId"];
                menu.ProgramName = dr["ProgramName"].ToString();
                menu.ImagePath = dr["ImagePath"].ToString();
                menu.ProgramPath = dr["ProgramPath"].ToString();
                menu.UseId = (int)dr["UseId"];
                menu.ParentId = (int)dr["ParentId"];
                menu.SortNum = (int)dr["SortNum"];
                menu.ShowToolBar = (int)dr["ShowToolBar"];
                list.Add(menu);
            }
            return list;
        }
        #endregion

        #region 根据用户查询相应显示的菜单
        /// <summary>
        /// 根据用户查询相应显示的菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Programs> QueryMenusAll(int userId)
        {
            //所有菜单项集合
            List<Programs> list = new List<Programs>();
            string sql = string.Format("select * from v_Program where userId={0}", userId);
            SqlDataReader dr = db.ExecReader(sql);
            //判断是否已经存在菜单项
            bool isNotSave = false;
            while (dr.Read())
            {
                Programs menu = new Programs();
                menu.ProgramId = (int)dr["ProgramId"];
                menu.ProgramName = dr["ProgramName"].ToString();
                menu.ImagePath = dr["ImagePath"].ToString();
                menu.ProgramPath = dr["ProgramPath"].ToString();
                menu.UseId = (int)dr["UseId"];
                menu.ParentId = (int)dr["ParentId"];
                menu.SortNum = (int)dr["SortNum"];
                menu.ShowToolBar = (int)dr["ShowToolBar"];
                foreach (Programs pro in list)
                {
                    if (pro.ProgramId == menu.ProgramId)
                    {
                        isNotSave = true;
                        break;
                    }
                    else
                    {
                        isNotSave = false;
                    }
                }
                if (isNotSave)
                {
                    continue;
                }
                else
                {
                    list.Add(menu);
                }
            }
            return list;
        }
        #endregion

        
        
    }
}
